<?php

class Staff extends MY_Admin_Controller
{
    public function index()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	$limit = 20;
    	
    	$this->load->model('staff/Model_staff', 'staff');
    	$total_rows = $this->staff->count();
    	
    	//分页
    	$this->load->library('pagination');
    	
    	$config['total_rows'] = $total_rows;
    	$config['per_page'] = $limit;
    	$config['base_url'] = '/rbac/staff/index';
    	
    	$this->pagination->initialize($config);
    	$show_page = $this->pagination->create_links();
    	
    	//读取数据
    	$offset = ($page-1) * $limit;
        $lists = $this->staff->lists($offset, $limit);
        
        $this->load->model('staff/Model_staff_depart', 'depart');
        $depart = $this->depart->getAllLists();
        
        $role_query = $this->db->query("SELECT id,rolename FROM sk_rbac_role WHERE status = 1");
        $role_data = $role_query->result_array('id');

        $data = array(
            'lists' => $lists,
        	'depart' => $depart,
        	'show_page'   => $show_page,
        	'total_rows'  => $config['total_rows'],
        	'role_data' => $role_data,
        );
        
        $this->load->view('admin/rbac/staff/index', $data);
    }
    
    public function work_time()
    {
    	$limit  = 20;
    	$page   = intval($this->input->get_post('page'));
    	$page   = $page ? $page : 1;
    	$offset = ($page - 1) * $limit;
    	$sid    = (int)$this->input->get_post('sid');
    
    
    	$where = array();
    	$sid && $where['sid'] = $sid;
    
    	if($this->input->get_post('recommend_uid'))
    	{
    		$where['recommend_uid'] = intval($this->input->get_post('recommend_uid'));
    	}
    
    	$this->load->model('staff/Model_staff_work','staff_work');
    	$total_rows = $this->staff_work->count($where);
    
    
    	//分页
    	$this->load->library('pagination');
    
    	$config['total_rows'] = $total_rows;
    	$config['per_page'] = $limit;
    	$config['base_url'] = '/rbac/staff/work_time';
    
    	$this->pagination->initialize($config);
    	$show_page = $this->pagination->create_links();
    
    
    	$list = $this->staff_work->lists($offset, $limit, $where);
    
    
    	$data = array
    	(
    			'infoAll'    => $list,
    			'show_page'  => $show_page,
    			'total_rows' => $config['total_rows'],
    	);
    
    	$this->load->view('admin/rbac/staff/work_time', $data);
    }

    public function create()
    {
        $this->load->model('staff/Model_staff_depart', 'depart');
        $depart = $this->depart->getAllLists();
        
        $role_query = $this->db->query("SELECT id,rolename FROM sk_rbac_role WHERE status = 1");
        $role_data = $role_query->result_array();
		
        $data = array(
            'depart' => $depart,
        	'role_data' => $role_data,
        );

        $this->load->view('admin/rbac/staff/create', $data);
    }

    public function detail()
    {
        $sid = $this->input->get_post('sid');
        $this->load->model('staff/Model_staff', 'staff');
        $staffInfo = $this->staff->getById($sid);

        $this->load->model('staff/Model_staff_depart', 'depart');
        $departmentInfo = $this->depart->lists(0, 1000, array('is_del' => DEPART_IS_DELETE_NORMAL));

        $data = array(
            'data' => $staffInfo,
            'departmentInfo' => $departmentInfo,
        );

        $this->load->view('admin/rbac/staff/detail', $data);
    }

    public function edit()
    {
        $sid = $this->input->get_post('sid');
        
        $this->load->model('staff/Model_staff', 'staff');
        $staffInfo = $this->staff->getById($sid);

        $this->load->model('staff/Model_staff_depart', 'depart');
        $depart = $this->depart->getAllLists();
        
        $role_query = $this->db->query("SELECT id,rolename FROM sk_rbac_role WHERE status = 1");
        $role_data = $role_query->result_array();

        $data = array(
            'data' => $staffInfo,
        	'depart' => $depart,
        	'role_data' => $role_data,
        );

        $this->load->view('admin/rbac/staff/create', $data);
    }
    
    public function status()
    {
    	$response = array('code' => '200', 'msg' => '操作成功');
    	
    	$sid = $this->input->get_post('sid');
    	$is_quit = $this->input->get_post('is_quit');
    
    	$data = array(
    			'is_quit' => $is_quit,
    	);
    
    	$this->load->model('staff/Model_staff', 'staff');
    	$this->staff->save($data, $sid);
    	
    	$this->json_output($response);
    }

    public function save()
    {
    	$sid = $this->input->get_post('sid');
    	$did = $this->input->get_post('did');
        $loginName = $this->input->get_post('login_name');
        $password = $this->input->get_post('password');
        $realname = $this->input->get_post('realname');
        $mobile = $this->input->get_post('mobile');
        $email = $this->input->get_post('email');
        $role_id = $this->input->get_post('role_id');
        
        
        $data = array(
        	'did' => $did,
        	'realname' => $realname,
            'login_name' => $loginName,
            'mobile' => $mobile,
            'email' => $email,
        	'role_id' => $role_id,
        );
        
        if($password)
        {
        	$data['password'] = md5($password);
        }
        
        $this->load->model('staff/Model_staff', 'staff');
        
        if($sid)
        {
        	$this->staff->save($data, $sid);
        }else{
        	$this->staff->save($data);
        }

        success_redirct("/rbac/staff/index","提交成功！");
    }

    public function delete()
    {

        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $sid = (int)$this->input->get_post('staff_id');
            $status   = (int)$this->input->get_post('status');


            if (empty ($sid)) {
                $response = error(100000);//参数不全
                break;
            }


            if (!array_key_exists($status, config_item('is_quit')))
                $status = IS_QUIT_QUIT;


            $this->load->model('staff/Model_staff', 'staff');
            $item = $this->staff->getById($sid);
            if (empty ($item))
            {
                $response = error(600002);
                break;
            }


            $rs = $this->staff->save(array('is_quit' => $status), $sid);


            if (!$rs) {
                $response = error(600003);
                break;
            }

        } while (false);

        $this->json_output($response);
    }
}
    